
#libraries

library(readxl)
library(openxlsx)   
library(tidyverse)
library(FactoMineR)
library(factoextra)
library(ggplot2)

#file

ÖVP <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_MCA.xlsx")


                                        ###############
                                        ###ÖVP#########
                                        ###############


# turn into factors

ÖVP <- ÖVP %>% 
  mutate_if(is.character,as.factor)

# data summary

summary(ÖVP)[, 3:22]

for (i in 3:22) {
  plot(ÖVP[,i], main=colnames(ÖVP)[i],
        ylab = "Count", col="steelblue", las = 2)
}

# visualize active variables

res.mca<-MCA(ÖVP,quali.sup=1:5)
print(res.mca)
plot(res.mca)

#results

var <- get_mca_var(res.mca)
var

#eigenvalues and modified rate

eig.val <- get_eigenvalue(res.mca)
eig.val

# screeplot

fviz_screeplot(res.mca, addlabels = TRUE, ylim = c(0, 20))

#contributions

head(round(var$contrib,2),50)

# MCA plot

factor_1_2  <- fviz_mca_var(res.mca, 
                            axes=c(1,2),
                            col.var = "black",
                            #col.quali.sup = "dimgray",
                            invisible=c("quali.sup"),
                            repel=TRUE,
                            labelsize = 3.0,
                            select.var=list(contrib=20), font.family = "sans")+
  theme_minimal()+
  labs(title = "ÖVP: categories")
factor_1_2

##clustering

res.hcpc <- HCPC(res.mca,order=FALSE) # 4 clusters

#dendrogram

fviz_dend(res.hcpc, show_labels = FALSE,
          palette = "Spectral")


# description by variable categories/individuals

res.hcpc$desc.var$category
res.hcpc$desc.ind


#save clusters

ÖVP_cluster<- res.hcpc$data.clust
ÖVP_cluster <- ÖVP_cluster %>%
  select(clust, everything())

ÖVP_cluster$LastName <- str_remove_all(ÖVP_cluster$LastName, "LastName_")
ÖVP_cluster$FirstName <- str_remove_all(ÖVP_cluster$FirstName, "FirstName_")

levels(ÖVP_cluster$clust)[levels(ÖVP_cluster$clust)=="1"] <- "Rural Fraction"
levels(ÖVP_cluster$clust)[levels(ÖVP_cluster$clust)=="2"] <- "Party Locals"
levels(ÖVP_cluster$clust)[levels(ÖVP_cluster$clust)=="3"] <- "Economic Fraction"
levels(ÖVP_cluster$clust)[levels(ÖVP_cluster$clust)=="4"] <- "Party Animals"

#visualize clusters

res.mca<-MCA(ÖVP_cluster,quali.sup=1:6)
print(res.mca)
plot(res.mca)

Cluster1_4_ÖVP <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "clust",
                           addEllipses=TRUE, ellipse.level=0.80, palette = "Spectral",font.family = "sans",legend.title = "Types")+
  theme_minimal()+
  labs(title = "ÖVP: individuals")

Cluster1_4_ÖVP 

#visualize passive variables 

ÖVP_cp <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Career Politicians",
                           addEllipses=TRUE, ellipse.level=0.70, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Career Politicians")+
  theme_minimal()+
  labs(title = "ÖVP")

ÖVP_cp

ÖVP_ps <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Public Sector Jobs",
                          addEllipses=TRUE, ellipse.level=0.70, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Public Sector Jobs")+
  theme_minimal()+
  labs(title = "")

ÖVP_ps

ÖVP_f <- fviz_pca_ind(res.mca, axes=c(1,2),label="none", habillage = "Farmers",
                        addEllipses=TRUE, ellipse.level=0.80, palette = c("#999999", "#56B4E9"),font.family = "sans",legend.title = "Farmers")+
  theme_minimal()+
  labs(title = "")

ÖVP_f

#export file with clusters

write.xlsx(ÖVP_cluster, file = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_cluster_final.xlsx")

